/**
 * 加减乘除算法
 * isNaN:判断一个值是非数字值
*/
const obj = {
    '+': 1,
    "-": 1,
    "*": 2,
    "/": 2,
    "^": 3
}
//(5+5-3)*2
function computed(data) {
    const output = [] // 5 5 + 3 - 2 *
    const icons = [] //( -
    data.match(/(\d+\.?\d*)|[+\-*/^()]/g).map(icon => {
        if (!isNaN(icon)) {
            output.push(icon)
        } else if (icon === '(') {
            icons.push(icon)
        } else if (icon === ')') {
            while (icons.length && icons[icons.length - 1] !== '(') {
                output.push(icons.pop())
            }
            icons.pop(icon)
        } else {
            while (icons.length && obj[icons[icons.length - 1]] >= obj[icon]) {
                output.push(icons.pop())
            }
            icons.push(icon)
        }
    })
    while (icons.length) {
        output.push(icons.pop())
    }
    return output
}
function evalFn(data) {
    const stack = []
    data.map(item => {
        if (!isNaN(item)) {
            stack.push(parseFloat(item))
        } else {
            const a = stack.pop()
            const b = stack.pop()
            switch (item) {
                case "+":
                    stack.push(a + b)
                    break
                case '-':
                    stack.push(b - a)
                    break
                case '*':
                    stack.push(a * b)
                    break
                case '/':
                    stack.push(b / a)
                    break
                case '^':
                    stack.push(Math.pow(b,a))
                    break
            }
        }
    })
    return stack.pop()
}
const data = computed('(5+5-3)*2^2')
console.log(data, 'data')  //  5 5 + 3 2 * - 
console.log(evalFn(data))